﻿package core.dialog
{

	import flash.display.MovieClip;
	import base.BaseMc;
	import core.display.AlphaBg;
	import core.net.socket.cmd.BluetoothConnectCmd;
	import core.events.CmdEvent;
	import flash.events.MouseEvent;
	import core.net.socket.cmd.BluetoothDisConnectCmd;
	import core.User;
	import core.events.DialogViewBluetoothEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import core.events.FlowerUploadEvent;
	import core.events.BlueToothConnectEvent;
	public class DialogBluetooth extends BaseMc
	{
		
		public static var connected:Boolean = false;
		
		var failed:DialogViewBluetoothConnectFailed;
		var setting:DialogViewBluetoothSetting;
		var address:String;
		private var bg:AlphaBg;
		
		private var txtConnecting:TextField;
		
		public function DialogBluetooth(address:String)
		{
			this.address=address;
			
		}
		protected override function onInit():void
		{
			bg = new AlphaBg();
			this.parent.addChild(bg);
			this.parent.swapChildren(bg,this);
			initFailedView();
			initSettingView();
			initTxtConnecting();
			
			this.scale(1.5,1.5);
			this.x = (this.stage.stageWidth - this.width) / 2;
			this.y = (this.stage.stageHeight - this.height) / 2; 
			
			this.btnClose.addEventListener(MouseEvent.CLICK,onCloseClick);
			
			trace("DialogBluetooth.connected:"+DialogBluetooth.connected);
			if(DialogBluetooth.connected)
			{
				this.failed.hide();
				this.setting.show();
				return;
			}
			else
			{
				trace("可直连");
				this.sendCmd(BluetoothConnectCmd.make(this.address));
			}
			
		}
		private function initTxtConnecting():void {
			txtConnecting = new TextField();
			txtConnecting.x = 260;
			txtConnecting.y = 200;
			txtConnecting.width = 200;
			txtConnecting.height = 100;
			var tf:TextFormat = new TextFormat();
			tf.size = 25;
			tf.color = 0x666666;
			tf.bold = true;
			txtConnecting.defaultTextFormat = tf;
			txtConnecting.text = "正在连接...";
			this.addChild(txtConnecting);
		}
		protected override function onRemove():void {
			super.onRemove();
			this.btnClose.removeEventListener(MouseEvent.CLICK,onCloseClick);
			this.sendCmd(BluetoothDisConnectCmd.make());
			this.bg.remove();
			DialogBluetooth.connected = false;
		}
		
		private function onCloseClick(e:MouseEvent):void {
			this.remove();
		}
		
		private function initFailedView():void
		{
			this.failed=new DialogViewBluetoothConnectFailed();
			this.failed.addEventListener(DialogViewBluetoothEvent.CONNECTING,onFailedConnecting);
			
			this.addChild(this.failed);
			this.failed.hide();
		}
		
		private function onFailedConnecting(e:DialogViewBluetoothEvent):void{
			txtConnecting.visible = true;
			this.sendCmd(BluetoothConnectCmd.make(this.address));
			this.failed.hide();
		}
		
		
		private function initSettingView():void
		{
			this.setting=new DialogViewBluetoothSetting();
			this.setting.addEventListener(FlowerUploadEvent.SUCCESS,flowerSuccess);
			this.setting.addEventListener(FlowerUploadEvent.UPLOADING,flowerUploading);
			this.addChild(this.setting);
			this.setting.hide();
		}
		//离线上传成功后添加关闭事件
		private function flowerSuccess(e:FlowerUploadEvent):void
		{
			 this.btnClose.addEventListener(MouseEvent.CLICK,onCloseClick);
		}
		//离线上传中取消关闭事件
		private function flowerUploading(e:FlowerUploadEvent):void
		{
			 this.btnClose.removeEventListener(MouseEvent.CLICK,onCloseClick);
		}
		protected override function onCommandResponse(e:CmdEvent):void {
			super.onCommandResponse(e);
			var data:Object = e.data;
			  if(data.cmd == BluetoothConnectCmd.COMMAND_ID){
				if(data.error == "") {
					trace("连接成功");
					this.removeChild(txtConnecting);
					DialogBluetooth.connected = true;
					this.failed.hide();
					this.setting.show();
					this.dispatchEvent(new BlueToothConnectEvent(BlueToothConnectEvent.BLUETOOTHCONNECT));
				}
				else{
					txtConnecting.visible = false;
					this.failed.show();
					this.setting.hide();
				}
			}
			
		}
	}

}